package org.example.config.sequence.configs;

import org.example.config.sequence.configs.beans.MyBeanPostProcessorBean;
import org.example.config.sequence.configs.beans.MyConfigurationBean;
import org.example.config.sequence.configs.definition.MyDeferredImportSelector;
import org.example.config.sequence.configs.definition.MyImportBeanDefinitionRegistrar;
import org.example.config.sequence.configs.definition.MyImportSelector;
import org.example.config.sequence.inspect.Inspector;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

/**
 * {@link Configuration @Configuration}本身也是一个{@link Component @Component}
 * <p>
 * 如果{@code Bean}名称相同，通过{@link ComponentScan @ComponentScan}扫描，仅会加载一次，如果名称不同，则会加载多个实例。
 */
@Configuration
//@ComponentScan(
//        basePackages = {"org.example.config.configs.beans"},
//        nameGenerator = MyBeanNameGenerator.class
//)
@Import({MyDeferredImportSelector.class, MyImportBeanDefinitionRegistrar.class, MyImportSelector.class})
public class SpringConfigAutoConfig implements ApplicationContextAware, EnvironmentAware, BeanFactoryAware, ResourceLoaderAware, BeanClassLoaderAware {
    @Bean
    public MyConfigurationBean myConfigurationBean() {
        Inspector.log(SpringConfigAutoConfig.class, "creating myConfigurationBean");
        return new MyConfigurationBean();
    }

    /**
     * 参见{@link MyBeanPostProcessorBean}处的说明
     */
    @Bean
    public MyBeanPostProcessorBean myBeanPostProcessorBean2() {
        return new MyBeanPostProcessorBean();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Inspector.log(SpringConfigAutoConfig.class, "setApplicationContext");
    }

    @Override
    public void setEnvironment(Environment environment) {
        Inspector.log(SpringConfigAutoConfig.class, "setEnvironment");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        Inspector.log(SpringConfigAutoConfig.class, "setBeanFactory");
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        Inspector.log(SpringConfigAutoConfig.class, "setResourceLoader");
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        Inspector.log(SpringConfigAutoConfig.class, "setBeanClassLoader");
    }
}
